package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Day;
import com.hadwinling.blog.entity.Message;
import com.hadwinling.blog.service.DayService;
import com.hadwinling.blog.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/19 8:04
 */
@Controller
public class DayController {
    @Autowired
    private DayService dayService;
    @Autowired
    private MessageService messageService;
    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;


    /**
     * 跳转到每日一言页面
     * @param model
     * @param pageNum
     * @return
     */
    @GetMapping("/admin/day")
    public String toDayPage(Model model, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum){
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Day> dayList = dayService.selectAllByStateOrderByCreatetime(1);
        PageInfo<Day> pageInfo = new PageInfo<>(dayList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("dayList",dayList);
        return "AdminCenter/day";


    }

    /**
     * 跳转到修改标签页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/admin/updateDay/{id}")
    public String toUpdateDayPage(@PathVariable(name = "id") int id,Model model){
        Day day = dayService.selectByPrimaryKey(id);
        model.addAttribute("day",day);
        return "AdminCenter/dayUpdate";
    }

    /**
     * 执行修改每日一言操作
     * @param day
     * @return
     */
    @PostMapping("/admin/updateDay")
    public String updateDay(Day day){
//        System.out.println(day);
        dayService.updateByPrimaryKeySelective(day);
        return "redirect:/admin/day";
    }

    /**
     * 跳转到发布每日一言页面
     * @return
     */
    @GetMapping("/admin/addDay")
    public String toAddDayPage(){
        return "AdminCenter/dayInput";
    }

    /**
     * 执行发布每日一言操作
     * @param day
     * @param model
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/admin/addDay")
    public String addDay(Day day, Model model, HttpServletRequest request,
                         HttpServletResponse response){
//        System.out.println("day:"+day);
        Date creatTime = new Date();
        day.setCreatetime(creatTime);
        day.setState(1);
        day.setTop(0);
        dayService.insertSelective(day);
/*        Message message = new Message();
        message.setState(1);
        message.setIsread(0);
        message.setNoticeid(day.getId());
        message.setCreatetime(new Date());
        messageService.insertSelective(message);*/
        return "redirect:/admin/day";
    }

    /**
     * 执行删除操作
     * @param dayid
     * @return
     */
    @PostMapping("/admin/deleteDay")
    @ResponseBody
    public String deleteDay(@RequestParam(name = "dayid") int dayid){
//        System.out.println("dayid:"+dayid);
        Day day = dayService.selectByPrimaryKey(dayid);
        day.setState(0);
        dayService.updateByPrimaryKeySelective(day);
        return "1";//表示删除成功
    }


    /**
     * 执行置顶操作
     * @param dayid
     * @return
     */
    @PostMapping("/admin/addDayTop")
    @ResponseBody
    public String addDayTop(@RequestParam(name = "dayid") int dayid){
        List<Day> dayTopList = dayService.selectAllByStateAndTop(1,1);
        if (dayTopList.size()==1){
            //不能置顶了
            return "0";
        }
        //执行置顶操作
        dayService.updateTopById(1,dayid);
        return "1";
    }

    /**
     * 执行取消置顶操作
     * @param dayid
     * @return
     */
    @PostMapping("/admin/noDayTop")
    @ResponseBody
    public String noDayTop(@RequestParam(name = "dayid") int dayid){
        List<Day> dayTopList = dayService.selectAllByStateAndTop(1,1);
        if (dayTopList.size()==1){
            //执行取消置顶操作
            dayService.updateTopById(0,dayid);
            return "1";
        }
        //不能执行取消置顶操作
        return "0";

    }



}
